package com.micro.core.handler;



import com.micro.common.MicroConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2019/11/22
 */
public class BaseContextHandler {

    /**
     * 定义线程池
     */
    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    /**
     * 为线程池赋值
     * @param key
     * @param value
     */
    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    /**
     * 从线程池获取值
     * @param key
     * @return
     */
    public static Object get(String key){
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    /**
     * 返回值
     * @param value
     * @return
     */
    private static String returnObjectValue(Object value) {
        return value==null?null:value.toString();
    }

    /**
     * 从线程池中移除
     */
    public static void remove(){
        threadLocal.remove();
    }


    /**
     * 获取token信息
     * @return
     */
    public static String getToken(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_APP_TOKEN);
        return returnObjectValue(value);
    }

    /**
     * 设置token
     * @param token
     */
    public static void setToken(String token){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_APP_TOKEN,token);
    }

    /**
     * 获取客户编号
     * @return
     */
    public static String getCustomerCode(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_CODE);
        return returnObjectValue(value);
    }

    /**
     * 设置客户编号
     * @param customerCode
     */
    public static void setCustomerCode(String customerCode){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_CODE,customerCode);
    }

    /**
     * 获取客户类型
     * @return
     */
    public static String getCustomerType(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_TYPE);
        return returnObjectValue(value);
    }

    /**
     * 设置客户类型
     */
    public static void setCustomerType(String customerType){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_TYPE,customerType);
    }

    /**
     * 获取客户主键id
     * @return
     */
    public static Long getCustomerId(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_ID);
        return value==null ? null : Long.valueOf(value.toString());
    }

    /**
     * 设置客户主键id
     * @param customerId
     */
    public static void setCustomerId(Long customerId){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_ID,customerId);
    }

    /**
     * 获取客户名称
     * @return
     */
    public static String getCustomerName(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_NAME);
        return returnObjectValue(value);
    }

    /**
     * 设置客户名称
     */
    public static void setCustomerName(String customerName){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CUSTOMER_NAME,customerName);
    }

    /**
     * 获取服务名
     * @return
     */
    public static String getServerName(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_SERVER_NAME);
        return returnObjectValue(value);
    }

    /**
     * 设置服务名
     * @param serverName
     */
    public static void setServerName(String serverName){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_SERVER_NAME,serverName);
    }

    /**
     * 获取渠道类型
     * @return
     */
    public static String getChannelType(){
        Object value = get(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CHANNEL_NAME);
        return returnObjectValue(value);
    }

    /**
     * 设置渠道类型
     * @param channelType
     */
    public static void setChannelType(String channelType){
        set(MicroConstant.ThreadLocalContext.CONTEXT_KEY_CHANNEL_NAME,channelType);
    }

}
